
package at.fhj.ase.clientarch.server.sigar;

import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.cmd.SigarCommandBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class providing the actual hardware information and operations.<br>
 * All methods within this class don't throw exceptions on purpose due to
 * <ul>
 * <li>convenience (no calling method has to handle exceptions)
 * <li>since there is now way of recovery in case of an exception
 * </ul>
 * In case of an exception, all the methods return {@link #INVALID_VALUE}.
 * <p>
 * Note that this class is <strong>not</strong> thread-safe and therefore should only be used by one thread.!<br>
 * The tests resulted in a {@link Double#NaN} if the value could not be read out properly.<br>
 * <p>
 * Class has package visibility and therefore should only be used by {@link SigarController}.
 * 
 * @author cja, sfe | www.fh-joanneum.at | Client Architectures and Design
 */
public class HardwareInfo extends SigarCommandBase {
  private Logger logger = LoggerFactory.getLogger(getClass().getName());
  public static final int INVALID_VALUE = -1;

  /**
   * Constructur just callable by {@link SigarController}.<br>
   * Package visibility prevents instantiaton outside of this package.
   */
  HardwareInfo() {

  }

  /** @return current CPU load in % */
  public double getCPULoad() {
    try {
      return sigar.getCpuPerc().getCombined() * 100;
    } catch (SigarException e) {
      logger.error("Could not fetch current CPU value", e);
      return INVALID_VALUE;
    }
  }

  /** @return current RAM load in % */
  public double getRAMLoad() {
    try {
      return sigar.getMem().getUsedPercent();
    } catch (SigarException e) {
      logger.error("Could not fetch current RAM value", e);
      return INVALID_VALUE;
    }
  }

  @Override
  public void output(String[] args) throws SigarException {
    // output not needed, therefore not implemented
  }
}
